package cn.iocoder.yudao.module.dbit.service.schinfo;

import cn.iocoder.yudao.module.dbit.controller.admin.majinfo.vo.MajInfoRespVO;
import cn.iocoder.yudao.module.dbit.dal.dataobject.majinfo.MajInfoDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.dbit.controller.admin.schinfo.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.schinfo.SchInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dbit.dal.mysql.schinfo.SchInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dbit.enums.ErrorCodeConstants.*;

/**
 * 共建学校 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SchInfoServiceImpl implements SchInfoService {

    @Resource
    private SchInfoMapper schInfoMapper;

    @Override
    public Integer createSchInfo(SchInfoSaveReqVO createReqVO) {
        // 插入
        SchInfoDO schInfo = BeanUtils.toBean(createReqVO, SchInfoDO.class);
        schInfoMapper.insert(schInfo);
        // 返回
        return schInfo.getSchId();
    }

    @Override
    public void updateSchInfo(SchInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateSchInfoExists(updateReqVO.getSchId());
        // 更新
        SchInfoDO updateObj = BeanUtils.toBean(updateReqVO, SchInfoDO.class);
        schInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteSchInfo(Integer id) {
        // 校验存在
        validateSchInfoExists(id);
        // 删除
        schInfoMapper.deleteById(id);
    }

    private void validateSchInfoExists(Integer id) {
        if (schInfoMapper.selectById(id) == null) {
            throw exception(SCH_INFO_NOT_EXISTS);
        }
    }

    @Override
    public SchInfoDO getSchInfo(Integer id) {
        return schInfoMapper.selectById(id);
    }

    @Override
    public PageResult<SchInfoDO> getSchInfoPage(SchInfoPageReqVO pageReqVO) {
        return schInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SchInfoDO> getSchInfoList() {
        return schInfoMapper.selectList();
    }

    @Override
    public List<MajInfoRespVO> getMajorList(Integer schId) {
        if (schId != null) {
            return schInfoMapper.getMajorList(schId);
        }
        return null;
    }

}